package com.bh.vehicle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.vehicle.domain.JcVehicle;
import com.bh.vehicle.mapper.JcVehicleMapper;
import com.bh.vehicle.service.IJcVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 车型管理Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcVehicleServiceImpl extends ServiceImpl<JcVehicleMapper, JcVehicle> implements IJcVehicleService {
    @Autowired
    private JcVehicleMapper jcVehicleMapper;

    /**
     * 查询车型管理
     *
     * @param id 车型管理主键
     * @return 车型管理
     */
    @Override
    public JcVehicle selectJcVehicleById(Long id) {
        return jcVehicleMapper.selectById(id);
    }

    /**
     * 查询车型管理列表
     *
     * @param jcVehicle 车型管理
     * @return 车型管理
     */
    @Override
    public List<JcVehicle> selectJcVehicleList(JcVehicle jcVehicle) {
        return jcVehicleMapper.selectJcVehicleList(jcVehicle);
    }

    /**
     * 新增车型管理
     *
     * @param jcVehicle 车型管理
     * @return 结果
     */
    @Override
    public int insertJcVehicle(JcVehicle jcVehicle) {
        if (jcVehicleMapper.selectCodeUnique(jcVehicle.getCode()) != null) {
            throw new ServiceException("车型编码已存在");
        }
        jcVehicle.setCreateTime(DateUtils.getNowDate());
        return jcVehicleMapper.insert(jcVehicle);
    }

    /**
     * 修改车型管理
     *
     * @param jcVehicle 车型管理
     * @return 结果
     */
    @Override
    public int updateJcVehicle(JcVehicle jcVehicle) {
        JcVehicle jcVehicle1 = jcVehicleMapper.selectCodeUnique(jcVehicle.getCode());
        if (jcVehicle1 != null && !jcVehicle1.getId().equals(jcVehicle.getId())) {
            throw new ServiceException("车型编码已存在");
        }
        return jcVehicleMapper.updateById(jcVehicle);
    }

    /**
     * 批量删除车型管理
     *
     * @param ids 需要删除的车型管理主键
     * @return 结果
     */
    @Override
    public int deleteJcVehicleByIds(Long[] ids) {
        return jcVehicleMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除车型管理信息
     *
     * @param id 车型管理主键
     * @return 结果
     */
    @Override
    public int deleteJcVehicleById(Long id) {
        return jcVehicleMapper.deleteById(id);
    }
}
